//
//  CreateTaskFormViewController.m
//  iModern
//
//  Created by Ralf on 6/1/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

#import "CreateTaskFormViewController.h"
#import "TaskManager.h"
#import "XLFormViewController+Helper.h"

@interface CreateTaskFormViewController ()
{
    TaskManager *taskManager;
}
@end

@implementation CreateTaskFormViewController

- (id)initWithData:(NSMutableDictionary *)data;
{
    self.task = data;
    taskManager = [TaskManager new];

    XLFormDescriptor        *form;
    XLFormSectionDescriptor *section;
    XLFormRowDescriptor     *row;

    form = [XLFormDescriptor formDescriptorWithTitle:@""];
    form.assignFirstResponderOnShow = YES;

    section = [XLFormSectionDescriptor formSection];

    [form addFormSection:section];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"message" rowType:XLFormRowDescriptorTypeTextView];
    row.title    = @"说明";
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"indexOfSaleTags" rowType:XLFormRowDescriptorTypeSelectorPush title:@"标签"];
    NSMutableArray      *options = [NSMutableArray new];
    XLFormOptionsObject *option  = [XLFormOptionsObject formOptionsObjectWithValue:@"妆发" displayText:@"妆发"];
    [options addObject:option];
    option = [XLFormOptionsObject formOptionsObjectWithValue:@"搭配" displayText:@"搭配"];
    [options addObject:option];
    option = [XLFormOptionsObject formOptionsObjectWithValue:@"陪购" displayText:@"陪购"];
    [options addObject:option];
    option = [XLFormOptionsObject formOptionsObjectWithValue:@"摄影" displayText:@"摄影"];
    [options addObject:option];
    option = [XLFormOptionsObject formOptionsObjectWithValue:@"纹身" displayText:@"纹身"];
    [options addObject:option];
    option = [XLFormOptionsObject formOptionsObjectWithValue:@"纹绣" displayText:@"纹绣"];
    [options addObject:option];
    row.title = @"主要分类";
    row.value = option;

    row.selectorOptions = options;
    row.required        = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"indexOfTags" rowType:XLFormRowDescriptorTypeText];
    row.required = YES;
    row.title    = @"次要分类";
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startTime" rowType:XLFormRowDescriptorTypeDateTimeInline];
    row.title    = @"开始时间";
    row.value    = [NSDate new];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"endTime" rowType:XLFormRowDescriptorTypeDateTimeInline];
    row.title    = @"截止时间";
    row.value    = [NSDate new];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"price" rowType:XLFormRowDescriptorTypeNumber];
    row.title    = @"单价(元)";
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"address" rowType:XLFormRowDescriptorTypeText];
    row.title    = @"服务区域";
    row.required = YES;
    [section addFormRow:row];

    return [super initWithForm:form];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initBackButton];
    [self initSaveButton];

    self.navigationItem.title = @"悬赏";
    // Do any additional setup after loading the view.
}

- (void)initSaveButton
{
    UIImage         *image  = [UIImage imageNamed:@"Save-Small.png"];
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(createTask:)];
    self.navigationItem.rightBarButtonItem = button;
}

- (IBAction)createTask:(id)sender
{
    if ([self.task[@"message"] isEqualToString:@"null"])
    {
        return;
    }

    if ([self.formValues[@"price"] isEqual:[NSNull null]])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请填写价格" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return;
    }

    int price = [self.formValues[@"price"] intValue];
    if (price < 0 || price > 10000)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"价格应该在0至10000元之间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return;
    }

    if ([self.formValues[@"times"] isEqual:[NSNull null]])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请填写接客量" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return;
    }

    if ([self.formValues[@"address"] isEqual:[NSNull null]])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请填写服务区域" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return;
    }

    if ([self.formValues[@"indexOfTags"] isEqual:[NSNull null]])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请填写关键字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return;
    }

    NSDate *startTime = self.formValues[@"startTime"];
    if ([startTime compare:[NSDate date]] == NSOrderedAscending)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"开始时间要晚于当前时间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }

    NSDate *endTime = self.formValues[@"endTime"];
    if ([endTime compare:startTime] == NSOrderedAscending)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"结束时间要晚于开始时间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }

    self.task[@"message"]     = self.formValues[@"message"];
    self.task[@"indexOfTags"] = self.formValues[@"indexOfTags"];
    self.task[@"startTime"]   = self.formValues[@"startTime"];
    self.task[@"endTime"]     = self.formValues[@"endTime"];
    self.task[@"address"]     = self.formValues[@"address"];
    self.task[@"price"]       = self.formValues[@"price"];
    XLFormOptionsObject *type = self.formValues[@"indexOfSaleTags"];
    self.task[@"indexOfSaleTags"] = type.valueData;

    self.task[@"type"] = @"Task";

    ResultSet *rs = [taskManager createTask:self.task];

    if (rs == nil)
    {
        [self alertNetworkConnectionException];
        return;
    }

    if ([rs hasErrors])
    {
        if (rs.errorCode == 3020)
        {
            [self alertUserStatusException];
        }
        else
        {
            [self alertWarningMessage:rs.errorMessage];
        }

        return;
    }

    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
